//
//  BoardController.m
//  HelloWorld
//
//  Created by Beomjun Kim on 09. 09. 21.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "BoardController.h"
#import "XmlUtil.h"
#import "XMLParser.h"
#import "TreeNode.h"
#import "BoardListModel.h"

#define URLSTRING_TAG		999

@implementation BoardController


@synthesize url;
@synthesize resultList;

-(BoardController *) initWithTitle: (id) title
						   withUrl: (NSString *) xmlUrl
{
	self = [super init];
	
	self.title = title;
	self.url = xmlUrl;
	
	[self.tabBarItem initWithTitle:self.title image:nil tag:0];
	
	return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return [self.resultList count];
}

- (void) modCell:(UITableViewCell *)aCell withIndex:(int)index
{
	BoardListModel *Board = [self.resultList objectAtIndex:index];
	
	// 이미지가 있을 경우에는 해당 URL로부터 직접 갖고 오고, 없을 경우에는 디폴트 이미지를 보여준다.
	//NSURL *url = [NSURL URLWithString:Board.image1];
	NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:Board.image1]];
	
	UIImage *image;
	
	if(data){
		image = [UIImage imageWithData:data];
		//[data release];
		
		UIImageView *view = [[UIImageView alloc] initWithImage:image];
		
        [view setFrame:CGRectMake(2.0f, 2.0f, 60.0f, 60.0f)];
        [aCell addSubview:view];
        
		[view release];
        //[image release];
		
	} else {
		//img =  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png" inDirectory:@"/"]];		
	}
	//[data autorelease];
	
	//aCell.image = image;
	
	
	// 이름에 해당하는 UILabel 생성
	CGRect tRect1 = CGRectMake(70.0f, 5.0f, 230.0f, 25.0f);
	UILabel *title1 = [[UILabel alloc] initWithFrame:tRect1];
	[title1 setText:Board.memo];
	[title1 setTextAlignment:UITextAlignmentLeft];
	[title1 setFont: [UIFont fontWithName:@"American Typewriter" size:18.0f]];
	[title1 setBackgroundColor:[UIColor clearColor]];
	
	// 남은시간에 해당하는 UILabel 생성
	CGRect tRect3 = CGRectMake(180.0f, 40.0f, 120.0f, 20.0f);
	UILabel *title3 = [[UILabel alloc] initWithFrame:tRect3];
	[title3 setText:[NSString stringWithFormat:@"작성자 : %@", Board.name]];
	[title3 setTextAlignment:UITextAlignmentRight];
	[title3 setFont: [UIFont fontWithName:@"Helvetica" size:14.0f]];
	[title3 setBackgroundColor:[UIColor clearColor]];
	
	// 꺽쇠 해당하는 UILabel 생성
	CGRect rightRect = CGRectMake(300.0f, 2.0f, 20.0f, 60.0f);
	UILabel *rightTitle = [[UILabel alloc] initWithFrame:rightRect];
	[rightTitle setText:@">"];
	[rightTitle setTextAlignment:UITextAlignmentLeft];
	//[rightTitle setTextColor:[UIColor redColor]];
	[rightTitle setFont: [UIFont boldSystemFontOfSize:14.0f]];
	[rightTitle setBackgroundColor:[UIColor clearColor]];
	
	// 셀에 UILabel을 추가한다
	[aCell addSubview:title1];
	[aCell addSubview:title3];	
	[aCell addSubview:rightTitle];	
	
	[title1 release];
	[title3 release];
	[rightTitle release];
}

- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	// 재사용 가능한 cell을 찾는다.
	UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:@"any-cell"];
	
	cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
	
	[self modCell:cell withIndex:[indexPath row]];
	
	return cell;}

/*
 *  Delegate Methods - This doesn't do anything, plus Apple has removed App launching from the SDK arsenal
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath 
{
	NSString *userSelection = [self.resultList objectAtIndex:[newIndexPath row]];
	printf("User selected %s\n",  [userSelection UTF8String]);
}


/*
 * 데이터 소스 설정
 */
-(void) setDataSource
{
	self.resultList = [[[NSMutableArray alloc] init] autorelease];

	// XML 결과를 가져온다.
	XMLParser *parser = [[XMLParser alloc] init];
	TreeNode *rss = [parser parseXMLFile:[NSURL URLWithString:self.url]];
		
	// root node
	TreeNode *root = [[rss children] objectAtIndex:0];
	
	int size = [[root children] count];	
	printf("Board list size : %d\n",  size);
	
	for(int i=0; i<size; i++) 
	{
		TreeNode *list = [[root children] objectAtIndex:i];
		
		if([list.key isEqual:@"board"]){
			BoardListModel *model = [[BoardListModel alloc] init];
			
			model.index = [XmlUtil stringForPath:@"index" ofNode:list];
			model.name = [XmlUtil stringForPath:@"name" ofNode:list];
			model.memo = [XmlUtil stringForPath:@"memo" ofNode:list];
			model.image1 = [XmlUtil stringForPath:@"image1" ofNode:list];
			
			[self.resultList addObject:model];
		} 
	}
}



- (void)loadView
{
	NSLog(@"BoardController loadView()");
	[super loadView];
	[self.tableView setRowHeight:80];
	
	NSLog(@"BoardController setDataSource()");
	[self setDataSource];
}

@end